package com.common.baselib.string;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static int[] getHMS(long timeStamp) { //时间戳转换成时分秒
        int times[] = new int[3];
        long sec = timeStamp / 1000;//转换成秒
        times[0] = (int) (sec / 3600);//小时
        int remainSec = (int) (sec % 3600);//剩下的秒数
        times[1] = remainSec / 60; //秒数转分钟
        times[2] = remainSec % 60; //秒数
        return times;
    }

    public static String getCurrentDate(String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }


    /**
     * 转化UTC时间格式
     *
     * @param time   utc格式的时间
     * @param format 希望转化的时间格式
     */
    public static String getByUTC(@NonNull String time, @Nullable String format) {
        String TIME_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
        String result = time;
        SimpleDateFormat sdf_in = new SimpleDateFormat(TIME_FORMAT_UTC);
        try {
            Date date = sdf_in.parse(time);
            if (TextUtils.isEmpty(format)) {
                format = "yyyy年MM月dd日 HH:mm";
            }
            SimpleDateFormat sdf_out = new SimpleDateFormat(format);
            result = sdf_out.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static String dateFormat(String time, String oldFormat, String newFormat) {
        String result = time;
        SimpleDateFormat sdf_in = new SimpleDateFormat(oldFormat);
        try {
            Date date = sdf_in.parse(time);
            SimpleDateFormat sdf_out = new SimpleDateFormat(newFormat);
            result = sdf_out.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断是否为今天(效率比较高)
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }



}
